package components.managers
{
    import components.core.UIComponent;
    import components.core.zn_internal;
    
    import flash.display.DisplayObject;
    import flash.display.Sprite;
    import flash.geom.Rectangle;
    import flash.utils.Dictionary;

    public class PopUpManager
    {
        //key:window value:boolean
        public static var windowList:Dictionary = new Dictionary(true);

        private static var _modelDic:Dictionary = new Dictionary(true);

        private static var _modelNum:int = 0;

        public function PopUpManager()
        {
        }

        public static function addPopUp(window:DisplayObject,
                                        model:Boolean = false):void
        {
            if (windowList[window])
                return;

            var app:SystemManager = SystemManager.instance;

            windowList[window] = true;

            if (model && !_modelDic[window])
            {
                _modelNum++;
                var sp:Sprite = app.zn_internal::addModalTransparency(_modelNum);
                _modelDic[window] = sp;
            }

            app.zn_internal::addPopChild(window);

            if (window is UIComponent)
                SystemManager.callLater(null, setFocusComp, [ window ]);
        }

        protected static function setFocusComp(window:UIComponent):void
        {
            FocusManager.currentFocusComp = window.focusComp;
        }

        public static function closeAll():void
        {
            //移除所有窗口
			
			var tempList:Dictionary=new Dictionary(true);
			var win:Object;
			for (win in windowList)
				tempList[win]=true;
			
            for (win in tempList)
            {
                removePopUp(win as DisplayObject);
            }
        }

        public static function removePopUp(window:DisplayObject):void
        {
            if (!windowList[window])
                return;

            var app:SystemManager = SystemManager.instance;

            app.zn_internal::removePopChild(window);

            delete windowList[window];

            if (_modelDic[window])
            {
                var comp:Sprite = _modelDic[window] as Sprite;
                _modelNum--;

                app.zn_internal::removeModalTransparency(_modelNum, comp);
                delete _modelDic[window];
            }

            if (window is UIComponent)
            {
                if (FocusManager.currentFocusComp == (window as UIComponent).focusComp)
                    FocusManager.currentFocusComp = null;
            }

        }

        public static function bringToFront(window:DisplayObject):void
        {
            if (!windowList[window])
                return;

            var app:SystemManager = SystemManager.instance;

            app.zn_internal::addPopChild(window);

            if (window is UIComponent)
                SystemManager.callLater(null, setFocusComp, [ window ]);

        }

        public static function centerPopUp(window:DisplayObject):void
        {
            if (!windowList[window])
                return;

            var app:SystemManager = SystemManager.instance;

			var rect:Rectangle=window.getBounds(window);
			
            window.x = app.width / 2 - rect.width*window.scaleX / 2;
            window.y = app.height / 2 - rect.height*window.scaleY / 2;
        }

    }
}